/*
 * @(#)ServletRequestUtil.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.util;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;


/**
 * @author Web Platform Development Team
 */
public class ServletRequestUtil {
	
    /**
     * {@link HttpServletRequest} 로부터의 parameter 정보를 이용해 queryString 생성.
     *
     * @param request the HttpServletRequest
     * @return the querystring
     */
    @SuppressWarnings("unchecked")
    public static String getQueryString(HttpServletRequest request) {
        StringBuilder queryString = new StringBuilder();
        Map<String, Object> parameters = request.getParameterMap();

        for (Entry<String, Object> entry : parameters.entrySet()) {

            if (queryString.length() > 0) {
                queryString.append("&");
            }

            String name = entry.getKey();
            String[] values = (String[])entry.getValue();

            for (int idx = 0; idx < values.length; idx++) {
                queryString.append(name).append("=").append(values[idx]);

                if ((idx + 1) != values.length) {
                    queryString.append("&");
                }

            }

        }

        return queryString.toString();
    }
}
